package org.sdp.pearproject.service.impl;


import org.sdp.pearproject.dto.convert.*;
import org.sdp.pearproject.dao.domain.*;
import org.sdp.pearproject.dto.request.*;
import org.sdp.pearproject.dto.response.*;
import org.sdp.pearproject.dao.mapper.*;
import org.sdp.pearproject.service.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: EventsServiceSuperImpl
 * @Description:
 * @Author: SDP
 * @Date: 2022-07-27
 * @Version: 1.0
 * Table: pear_events
 * Comment:
 * 日程表
 */
public class EventsServiceSuperImpl extends BaseServiceImpl implements EventsService {
    @Autowired
    protected EventsMapper eventsMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        map.put("code", "编号");
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addEvents(EventsAddRequest request) throws Exception {
        EventsWithBLOBs daoRequest = new EventsAddRequestConvert().convert(request);
        fixEqualCondition(request, "events", "add", "", null);
        Integer result = 0;
        try {
            result = eventsMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            result = daoRequest.getId();
        }
        addHistory(request, "A", "events", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateEvents(EventsUpdateRequest request) throws Exception {
        fixEqualCondition(request, "events", "update", request.getId()+"", null);
        checkInternalId(request, "events", "update", request.getId());
        addHistory(request, "U", "events", "api", request.getId(), null);

        EventsWithBLOBs daoRequest = new EventsUpdateRequestConvert().convert(request);
        EventsExample example = new EventsExample();
        example.createCriteria().andIdEqualTo(request.getId());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), EventsExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = eventsMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteEvents(EventsDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "events", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "events", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "events", "api", request.getIdList().get(index), null);
            eventsMapper.deleteByPrimaryKey(request.getIdList().get(index));
            count++;
        }
        return count;
    }

    @Override
    public EventsQueryResponse queryEvents(EventsQueryRequest request) throws Exception {
        EventsWithBLOBs daoResponse = eventsMapper.selectByPrimaryKeyWithBLOBs(request.getId());
        EventsQueryResponse response = new EventsQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<EventsQueryResponse> listEvents(EventsQueryRequest request) throws Exception {
        EventsWithBLOBs daoRequest = new EventsWithBLOBs();
        copyProperties(request, daoRequest);
        EventsExample example;
        try {
            example = EventsExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<EventsWithBLOBs> list;
        try {
            list = eventsMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<EventsQueryResponse> responseList = new ArrayList<>();
        for(EventsWithBLOBs item : list) {
            EventsQueryResponse newItem = new EventsQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countEvents(EventsQueryRequest request) throws Exception {
        EventsWithBLOBs daoRequest = new EventsWithBLOBs();
        copyProperties(request, daoRequest);
        EventsExample example;
        try {
            example = EventsExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = eventsMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statEvents(EventsQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public List<EventsForEventsResponse> events(EventsForEventsRequest request) throws Exception {
        EventsForEvents daoRequest = new EventsForEvents();
        copyProperties(request, daoRequest);
        
        List<EventsForEvents> list;
        
        EventsForEventsExample example;
        try {
            example = EventsForEventsExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            list = eventsMapper.eventsByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        List<EventsForEventsResponse> responseList = new ArrayList<>();
        for(EventsForEvents item : list) {
            EventsForEventsResponse newItem = new EventsForEventsResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }
        
        return responseList;
    }

    @Override
    public Integer countForEvents(EventsForEventsRequest request) throws Exception {
        EventsForEvents daoRequest = new EventsForEvents();
        copyProperties(request, daoRequest);
        
        Integer count;
        
        EventsForEventsExample example;
        try {
            example = EventsForEventsExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            count = eventsMapper.countEventsByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        return count;
    }
    
    @Override
    public BaseStatResponse statForEvents(EventsForEventsRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public List<EventsForEventsMemberResponse> eventsMember(EventsForEventsMemberRequest request) throws Exception {
        EventsForEventsMember daoRequest = new EventsForEventsMember();
        copyProperties(request, daoRequest);
        
        List<EventsForEventsMember> list;
        
        EventsForEventsMemberExample example;
        try {
            example = EventsForEventsMemberExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            list = eventsMapper.eventsMemberByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        List<EventsForEventsMemberResponse> responseList = new ArrayList<>();
        for(EventsForEventsMember item : list) {
            EventsForEventsMemberResponse newItem = new EventsForEventsMemberResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }
        
        return responseList;
    }

    @Override
    public Integer countForEventsMember(EventsForEventsMemberRequest request) throws Exception {
        EventsForEventsMember daoRequest = new EventsForEventsMember();
        copyProperties(request, daoRequest);
        
        Integer count;
        
        EventsForEventsMemberExample example;
        try {
            example = EventsForEventsMemberExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            count = eventsMapper.countEventsMemberByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        return count;
    }
    
    @Override
    public BaseStatResponse statForEventsMember(EventsForEventsMemberRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
